package com.cma.cmabackend.controller;

import com.cma.cmabackend.entity.Equipment;
import com.cma.cmabackend.service.ProjectService;
import com.cma.cmabackend.util.EquipmentParser;
import com.cma.cmabackend.util.FileProcess;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/file")
public class FileController {
    @Resource
    ProjectService projectService;

    @Resource
    FileProcess fileProcess;

    @PostMapping("")
    public String upload(MultipartFile file) {
        return FileProcess.save(file);
    }

    @PostMapping("/equipmentsheet")
    public List<Equipment> parser(MultipartFile file) {
        try {
            return EquipmentParser.parseEquipment(file);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @GetMapping("/generate")
    public List<String> generateExcel(int id) {
        List<String> result = new ArrayList<>();
        result.add(fileProcess.generateExcel1(projectService.findRealById(id)));
        result.add(fileProcess.generateExcel2(projectService.findRealById(id)));
        result.add(fileProcess.generateExcel3(projectService.findRealById(id)));
        return result;
    }
    @GetMapping("/excel1")
    public String generateExcel1(int id) {
        return fileProcess.generateExcel1(projectService.findRealById(id));
    }
    @GetMapping("/excel2")
    public String generateExcel2(int id) {
        return fileProcess.generateExcel2(projectService.findRealById(id));
    }
    @GetMapping("/excel3")
    public String generateExcel3(int id) {
        return fileProcess.generateExcel3(projectService.findRealById(id));
    }
}
